/*
 * Copyright 2014 Graham Smith
 */
package uk.co.wobblycogs.vegas.scan;

import java.nio.file.Path;
import java.time.Duration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author Graham Smith
 */
public class ScanResults {

	//A project maps to many pieces of media
	private final Map<Path, Set<Path>> projectMediaResults = new HashMap<>();
	//A piece of media can be used in many projects.
	private final Map<Path, Set<Path>> mediaProjectResults = new HashMap<>();
	private final Set<FileResource> mediaResults = new HashSet<>();
	private Duration duration;

	public ScanResults() {
	}

	public Map<Path, Set<Path>> getProjectMediaResults() {
		return projectMediaResults;
	}

	public Map<Path, Set<Path>> getMediaProjectResults() {
		return mediaProjectResults;
	}

	public Set<FileResource> getMediaResults() {
		return mediaResults;
	}

	/**
	 * Adds a project scanning result. The media path given here is the path
	 * stored in the Vegas project file.
	 *
	 * @param media
	 * @param project
	 */
	public void addProjectResult(Path project, Path media) {
		//Project to media entry
		Set<Path> m = projectMediaResults.get(project);
		if (m == null) {
			m = new HashSet<>();
			projectMediaResults.put(project, m);
		}
		m.add(media);

		//Media to project entry
		Set<Path> p = mediaProjectResults.get(media);
		if (p == null) {
			p = new HashSet<>();
			mediaProjectResults.put(media, p);
		}
		p.add(project);
	}

	public void addMediaResult(FileResource media) {
		mediaResults.add(media);
	}

	/**
	 * Finds all the media that isn't being used in any project.
	 *
	 * @return
	 */
	public Set<FileResource> getUnusedMedia() {
		Set<FileResource> unused = new TreeSet<>(mediaResults);
		unused.removeAll(mediaProjectResults.keySet());
		return unused;
	}

	public Duration getDuration() {
		return duration;
	}

	/**
	 * The total time the scan took.
	 *
	 * @param duration
	 */
	public void setDuration(Duration duration) {
		this.duration = duration;
	}

}
